/*
 * Copyright © Yolanda. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.baselibrary.libs.Utils.netUtils.NoHttp.queue;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.baselibrary.libs.Utils.log.LogUtils;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created in Mar 27, 2016 8:10:58 PM.
 * 
 * @author Yolanda;
 */
public abstract class YolandaLinkedQueue<Request,Param, Update, Result> {


	/**
	 * 更新的what
	 */
	public static final int WHAT_UPDATE = 0x01;

	/**
	 * 发送结果的what
	 */
	public static final int WHAT_RESULT = 0x02;
	/**
	 * 发送结果的what
	 */
	public static final int WHAT_PARAM = 0x03;
	/**
	 * 开始的what
	 */
	public static final int WHAT_START = 0x04;


	/**
	 * 发送结果的Handler
	 */
	private static Handler sHandler;

	/**
	 * Handler的锁
	 */
	private static Object HANDLER_LOCK = new Object();

	private BlockingQueue<Request> blockingQueue;
	/**
	 * 并发工作线程
	 */
	private TaskExecutor[] taskExecutors;

	public YolandaLinkedQueue(int poolSize) {
		// LinkedBlockingQueue是一个先进先出的队列
		blockingQueue = new LinkedBlockingQueue<>();
		taskExecutors = new TaskExecutor[poolSize];
	}

	public int getSize(){
		return blockingQueue.size();
	}

	public void add(Request t) {
		blockingQueue.add(t);
	}

	public Object CancelTag;

	public void start() {
		for (int i = 0; i < taskExecutors.length; i++) {
			taskExecutors[i] = new TaskExecutor(blockingQueue,this);
			taskExecutors[i].start();
		}
	}
	public void setCancelTag(Object o){
		this.CancelTag = o;


	}
	public void stop() {
		for (TaskExecutor taskExecutor : taskExecutors) {
			taskExecutor.setRunning(false);
			taskExecutor.interrupt();

		}
	}
	public boolean dump;
	public void setDump(boolean b){
		this.dump = b;
	}


	//子类 回调
	public abstract void getData(Request t, TaskExecutor<Request, Param, Update, Result> taskExecutor, YolandaLinkedQueue<Request, Param, Update, Result> queue);

	public  void onResult(Request request,Result result){
		Message message = getDefaultPoster().obtainMessage();
		message.what = WHAT_RESULT;
		message.obj = new Messages(request,null,null,result,this);
		message.sendToTarget();
	}
	public  void onUpData(Request request,Update update){
		Message message = getDefaultPoster().obtainMessage();
		message.what = WHAT_UPDATE;
		message.obj = new Messages(request,null,update,null,this);
		message.sendToTarget();
	}
	public  void onParam(Request request,Param param){
		Message message = getDefaultPoster().obtainMessage();
		message.what = WHAT_PARAM;
		message.obj = new Messages(request,param,null,null,this);
		message.sendToTarget();
	}
	public  void onStart(Request request,Result result){
		Message message = getDefaultPoster().obtainMessage();
		message.what = WHAT_START;
		message.obj = new Messages(request,null,null,result,this);
		message.sendToTarget();
	}


	public abstract void getResult(Request request,Result result);
	public abstract void getUpData(Request request, Update update);
	public abstract void getParam(Request request,Param param);
	public abstract void getStart(Request request,Result result);
	/**
	 * 获取一个单例 主线程handler
	 * @return
	 */
	public static Handler getDefaultPoster() {
		synchronized (HANDLER_LOCK) {
			if (sHandler == null)
				sHandler = new YolandaLinkedQueue.Mymsg();
			return sHandler;
		}
	}
	/**
	 * 主线程 handler
	 */
	public static class Mymsg extends Handler{
	public 	Mymsg(){
		super(Looper.getMainLooper());
	}
		@Override
		public void handleMessage(Message msg) {
			Messages<?, ?,?, ?> messages = (Messages<?,?, ?, ?>) msg.obj;
			LogUtils.i("我当前的线程");
			switch (msg.what){
				//更新的what
				case WHAT_UPDATE:
					messages.onUpData();
				break;
				//发送结果的what
				case WHAT_RESULT:
					messages.onResult();
				break;
				//失败的what
				case WHAT_PARAM:
					messages.onParam();
				break;
				//开始的what
				case WHAT_START:
					messages.onStart();
				break;
			}
		}
	}

}
